<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix='sec'
	uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
</head>
<body style="border: 0px; margin: 0px; padding: 0px;width: 100%;height: 100%">
	<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
	    <table style="width:100%;">
	    	<tr>
	    		<td style="width:100%;">
	            	<span>名称：</span>
	                <input id="name" class="mini-textbox" emptyText="请输入药品名称" style="width:120px;" onenter="onKeyEnter"/>   
	                <span>编号：</span>
	                <input id="serialNum" class="mini-textbox" emptyText="请输入编号" style="width:120px;" onenter="onKeyEnter"/>   
	                <span>厂家：</span>
	                <input id="manufacturer" class="mini-textbox" emptyText="请输入生产厂家名称" style="width:120px;" onenter="onKeyEnter"/>  
	            </td>
	            <td style="white-space:nowrap;">
                	<a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
                </td>
	    	</tr>
	        <tr>
	            <td>
	                <sec:authorize url='/medicine_add.do'>
	                <a class="mini-button" iconCls="icon-add" onclick="add()">增加</a>
                    </sec:authorize>
                    <sec:authorize url='/medicine_edit.do'>
	                <a class="mini-button" iconCls="icon-edit" onclick="edit()">编辑</a>
	                </sec:authorize>
	                <sec:authorize url='/medicine_del.do'>
	                <a class="mini-button" iconCls="icon-remove" onclick="remove()">删除</a>
	                </sec:authorize>
	                <sec:authorize url='/medicine_import_add.do'>
	                <a class="mini-button" iconCls="icon-add" onclick="importAdd()">批量导入</a>
	                </sec:authorize>
	                <sec:authorize url='/medicine_download.do'>
	                <a class="mini-button" iconCls="icon-download" href="medicine_download.do">下载导入模版</a>
	                </sec:authorize>
	            </td>
	            <td></td>
	        </tr>
	    </table>           
	</div>
	<div class="mini-fit" >
	<div id="datagrid1" class="mini-datagrid " idField="id" pageSize="20" url="medicine_view.do" style="width:100%;height:100%;" multiSelect="true">
		<div property="columns">
			<div type="indexcolumn"></div>
			<div type="checkcolumn"></div>
			<div field="serialNum" width="12%" headerAlign="center" align="center">编号</div>
			<div field="name" width="25%" headerAlign="center" align="center">名称/规格</div>
			<div field="aliases" width="12%" headerAlign="center" align="center">别名</div>
			<div field="manufacturer" width="20%" headerAlign="center" align="center">生产厂家</div>
			<div field="medicineTypeValue" width="12%" headerAlign="center" align="center">产品类型</div>
			<div name="action" width="19%" headerAlign="center" align="center" renderer="onActionRenderer" >相关操作</div>
		</div>
	</div>
	</div>
<script type="text/javascript">
	mini.parse();
	
	var grid = mini.get("datagrid1");
	grid.load();
	
    function search() {
        var name = mini.get("name").getValue();
        var serialNum = mini.get("serialNum").getValue();
        var manufacturer = mini.get("manufacturer").getValue();
        grid.load({ 
        	name: name,
        	serialNum: serialNum,
        	manufacturer: manufacturer,
        });
    }
	
	//相关操作
	function onActionRenderer(e) {
		return '<a href="javascript:viewDetail()">查看详细</a> <a href="javascript:viewLocation()">查看位置</a>';
	}
	
	function viewDetail(){
		 var row = grid.getSelected();
	        if (row) {
	            mini.open({
	                url: "medicine_see_details.do",
	                allowResize:false,
	                title: "药品信息", width: 730, height: 555,
	                onload: function () {
	                    var iframe = this.getIFrameEl();
	                    var data = { action: "see", row: row };
	                    iframe.contentWindow.SetData(data);
	                }
	            }); 
	        } 
	}
	
	function viewLocation(){
		 var row = grid.getSelected();
	        if (row) {
	            mini.open({
	                url: "medicine_view_location.do",
	                allowResize:false,
	                title: "药品信息", width: 730, height: 555,
	                onload: function () {
	                    var iframe = this.getIFrameEl();
	                    iframe.contentWindow.SetData(row);
	                }
	            }); 
	        } 
	}

	function add() {
	    mini.open({
	        url: "medicine_add.do",
	        title: "新增药品", width: 650, height: 600,
	        allowResize:false,
	        ondestroy: function (action) {
	        	if(action == 'succ'){
	        		mini.showMessageBox({
	                    title: "提示",
	                    showModal:false,
	                    iconCls: "mini-messagebox-info",
	                    message: "保存成功！",
	                    timeout: 3000,
	                    x: 'right',
	                    y: 'bottom'
	                });
		            grid.reload();
	        	}
	        }
	    });
	}
	
	function edit() {
        var row = grid.getSelecteds();
        if (row.length == 1) {
            mini.open({
                url: "medicine_edit.do",
                allowResize:false,
                title: "编辑药品信息", width: 650, height: 600,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "edit", row: row[0] };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                	if(action == 'succ'){
    	        		mini.showMessageBox({
    	                    title: "提示",
    	                    showModal:false,
    	                    iconCls: "mini-messagebox-info",
    	                    message: "修改成功！",
    	                    timeout: 3000,
    	                    x: 'right',
    	                    y: 'bottom'
    	                });
    		            grid.reload();
    	        	}
                }
            });
            
        } else {
            mini.alert("请选中一条记录");
        }
        
    }
	
    function remove() {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
        	mini.confirm("确定删除记录？", "确定？",function (action) {
        		if (action == "ok") {
        			var ids = [];
                    for (var i = 0, l = rows.length; i < l; i++) {
                        var r = rows[i];
                        ids.push(r.id);
                    }
                    var id = ids.join(',');
                    grid.loading("操作中，请稍后......");
                    $.get("medicine_del.do?id=" +id,function (text) {
                        	grid.unmask();
                        	if(text.success){
                        		mini.showMessageBox({
            	                    title: "提示",
            	                    showModal:false,
            	                    iconCls: "mini-messagebox-info",
            	                    message: "删除成功！",
            	                    timeout: 3000,
            	                    x: 'right',
            	                    y: 'bottom'
            	                });
                        		grid.reload();
                        	}else{
                        		mini.alert("该药品已经在订单中不能删除！");
                        	}
                        	if(text.message){
                        		mini.alert("药品id为空！");
                        	}
                        },"json");
        		}
        	});
        } else {
            mini.alert("请选中一条记录");
        }
    }
    
    function importAdd(){
    	mini.open({
	        url: "medicine_import_add.do",
	        allowResize:false,
	        title: "批量导入药品", width: 300, height: 250,
	        ondestroy: function (action) {
	        	if(action == 'succ'){
	        		mini.showMessageBox({
	                    title: "提示",
	                    showModal:false,
	                    iconCls: "mini-messagebox-info",
	                    message: "导入成功！",
	                    timeout: 3000,
	                    x: 'right',
	                    y: 'bottom'
	                });
	        		grid.reload();
	        	} else if(action == '数据错误') {
	        		mini.showMessageBox({
	                    title: "提示",
	                    showModal:false,
	                    iconCls: "mini-messagebox-info",
	                    message: "数据错误！",
	                    timeout: 3000,
	                    x: 'right',
	                    y: 'bottom'
	                });
	        	} else {
	        		mini.showMessageBox({
	                    title: "提示",
	                    showModal:false,
	                    iconCls: "mini-messagebox-info",
	                    message: action,
	                    timeout: 3000,
	                    x: 'right',
	                    y: 'bottom'
	                });
	        	}
	        }
	    });
    }
    
</script>
</body>
</html>